﻿using System;
using System.DHTML;
using Sys;
using Sys.UI;
using Sys.Mvc;

using JQuerySharp;
using VietPublisher.Scripts.Resources;

public delegate void SelectUserDeptReturnHandler(string userId, string userName, string empCode, string empName, string deptCode, string deptName, string email, string pstName);
public delegate void SelectDeptReturnHandler(string deptCode, string deptName);
public delegate void SelectMenuReturnHandler(string menuId, string menuName);
public delegate void SelectUserDeptReturnHandler_2(string userId, string userName, string empCode, string empName, string deptCode, string deptName, string email, string pstName, string mobilephone, string empnameeng, string companyphone);
static public class SC_Popup
{ 
    public static string ControllerUrl = "Popup";

    #region UserDepartmentPopup

    public static SelectUserDeptReturnHandler OnSelectUserEmp;
    public static SelectUserDeptReturnHandler OnAddUserEmp;
    public static SelectUserDeptReturnHandler_2 OnSelectUserEmp_2;
    /// <summary>
    /// Show popup
    /// </summary>
    /// <param name="searchName"></param>
    static public void OpenEmpDeptPopup(string searchName)
    {
        string href = SC_Common.BasePath + ControllerUrl + "/UserDepartmentPopup?searchText=" + searchName.Escape() + "&hasuserid=false" ;
        Utils.OpenWinCenter("UserDeptPop", href, 930, 680);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="searchName"></param>
    static public void OpenEmpDeptPopupMulti(string searchName)
    {
        // Return
        // 
        /*
         * SC_Popup.
         * 
         * */
        string href = SC_Common.BasePath + ControllerUrl + "/UserDepartmentPopupMulti?searchText=" + searchName.Escape() + "&hasuserid=false";
        Utils.OpenWinCenter("UserDeptPop", href, 930, 680);
    }

    /// <summary>
    /// Send by deptcode
    /// </summary>
    /// <param name="deptCode"></param>
    static public void OpenEmpDeptPopup2(string deptCode)
    {
        string href = SC_Common.BasePath + ControllerUrl + "/UserDepartmentPopup?type=0&searchText=" + deptCode.Escape() + "&hasuserid=false";
        Utils.OpenWinCenter("UserDeptPop", href, 930, 680);
    }

    static public void OpenUserDeptPopup(string searchName)
    {
        string href = SC_Common.BasePath + ControllerUrl + "/UserDepartmentPopup?searchText=" + searchName.Escape() + "&hasuserid=true";
        Utils.OpenWinCenter("UserDeptPop", href, 930, 680);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="searchName"></param>
    static public void OpenUserDeptPopupMulti(string searchName)
    {
        // Return
        // 
        /*
         * SC_Popup.
         * 
         * */
        string href = SC_Common.BasePath + ControllerUrl + "/UserDepartmentPopupMulti?searchText=" + searchName.Escape() + "&hasuserid=true";
        Utils.OpenWinCenter("UserDeptPop", href, 930, 680);
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="type">type=0; get user of department, =1; search user by empName, userName</param>
    /// <param name="text"></param>
    /// <param name="updateTargetId"></param>
    static public void GetListOfUsers(int type, string text, string updateTargetId)
    {
        string hasUserId = Utils.GetQueryString(Window.Location.Href, "hasuserid");
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/ListUsersOfDept?type={0}&searchtext={1}&hasuserid={2}&selectmulti={3}", 
                type, text.Escape(), hasUserId, Utils.GetInputValue("SelectMulti"));
        UIHelper.LoadAjax(href, updateTargetId, new MvcCallback(delegate(AjaxContext context){
            Script.Literal("InitCheckAll();");
            Script.Literal("GridEmpList_onDataBound();");            
        }), null);
    }


    /// <summary>
    /// 
    /// </summary>
    /// <param name="row"></param>
    static public void UserGrid_OnDblClickRow(TableRowElement row)
    {
        DOMElementCollection coll = row.GetElementsByTagName("span");
        for (int i = 0; i < coll.Length; i++)
        {
            if (coll[i].Attributes.GetNamedItem("name").Value == "spnEmail")
            {
                string empName = coll[i].Attributes.GetNamedItem("empname").Value;
                string deptName = coll[i].Attributes.GetNamedItem("deptname").Value;
                string empCode = coll[i].Attributes.GetNamedItem("empcode").Value;
                string email = coll[i].InnerText;

                string userId = coll[i].Attributes.GetNamedItem("userid").Value;
                string userName = coll[i].Attributes.GetNamedItem("username").Value;
                string deptCode = coll[i].Attributes.GetNamedItem("deptcode").Value;
                string pstName = coll[i].Attributes.GetNamedItem("psttypename").Value;

                string mobilephone = coll[i].Attributes.GetNamedItem("mobilephone").Value;
                string empnameeng = coll[i].Attributes.GetNamedItem("empnameeng").Value;
                string companyphone = coll[i].Attributes.GetNamedItem("companyphone").Value;

                string script = string.Format("SC_Popup.onSelectUserEmp('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}');",
                        userId, userName, empCode, empName, deptCode, deptName, email, pstName);
                string script_2 = string.Format("SC_Popup.onSelectUserEmp_2('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}', '{8}', '{9}', '{10}');",
                       userId, userName, empCode, empName, deptCode, deptName, email, pstName, mobilephone, empnameeng, companyphone);
                string s = "opener.setTimeout(\"if (SC_Popup.onSelectUserEmp != null) { " + script + "}\", 500);" ;
                Script.Eval(s);
                string s2 =  "opener.setTimeout(\"if (SC_Popup.onSelectUserEmp_2 != null) { " + script_2 + "}\", 500);";
                Script.Eval(s2);
                break;
            }
        }        
        Utils.CloseWindow();
    }

    /// <summary>
    /// Return multi checked users
    /// </summary>
    public static void OnSelectMultiUsers()
    {
        jQuery query = JQueryFactory.JQuery("#GridEmpMst table tbody input:checkbox[checked=true]");
        int count = (int)Type.GetField(query, "length");

        if (count > 0)
        {
            string empName = "";
            string deptName = "";
            string empCode = "";
            string email = "";

            string userId = "";
            string userName = "";
            string deptCode = "";
            string pstName = "";

            CheckBoxElement[] chkList = (CheckBoxElement[])(object)query;
            for (int k = 0; k < chkList.Length; k++)
            {
                DOMElement rowElement = chkList[k].ParentNode.ParentNode;


                DOMElementCollection coll = rowElement.GetElementsByTagName("span");
                for (int i = 0; i < coll.Length; i++)
                {
                    if (coll[i].Attributes.GetNamedItem("name").Value == "spnEmail")
                    {
                        empName += coll[i].Attributes.GetNamedItem("empname").Value;
                        empName += ";";

                        deptName += coll[i].Attributes.GetNamedItem("deptname").Value;
                        deptName += ";";
                        
                        empCode += coll[i].Attributes.GetNamedItem("empcode").Value;
                        empCode += ";";

                        email += coll[i].InnerText;
                        email += ";";

                        userId += coll[i].Attributes.GetNamedItem("userid").Value;
                        userId += ";";

                        userName += coll[i].Attributes.GetNamedItem("username").Value;
                        userName += ";";

                        deptCode += coll[i].Attributes.GetNamedItem("deptcode").Value;
                        deptCode += ";";

                        pstName += coll[i].Attributes.GetNamedItem("psttypename").Value;
                        pstName += ";";
                        break;
                    }
                }
            }

            string script = string.Format("SC_Popup.onSelectUserEmp('{0}', '{1}', '{2}', '{3}', '{4}', '{5}', '{6}', '{7}');",
                    userId, userName, empCode, empName, deptCode, deptName, email, pstName);
            string s = "opener.setTimeout(\"if (SC_Popup.onSelectUserEmp != null) { " + script + "}\", 500);";
            Script.Eval(s);
        }

        Utils.CloseWindow();
    }


    #endregion

    #region Department Popup

    public static SelectDeptReturnHandler OnSelectDept;

    /// <summary>
    /// Show and search
    /// </summary>
    /// <param name="searchText"></param>
    public static void OpenSelectDeptPopup(string searchText)
    {
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/DepartmentPopup?searchtext={0}", searchText.Escape());
        Utils.OpenWinCenter("DeptMstPop", href, 350, 450);
    }

    /// <summary>
    /// Return multi departments
    /// </summary>
    /// <param name="searchText"></param>
    public static void OpenSelectDeptPopupMulti(string searchText)
    {
        string href = SC_Common.BasePath + ControllerUrl + string.Format("/DepartmentPopup?check=1&searchtext={0}", searchText.Escape());
        Utils.OpenWinCenter("DeptMstPop", href, 350, 450);
    }

    #endregion

    #region Menu Pop

    public static SelectMenuReturnHandler OnSelectMenu;
    public static void OpenSelectMenuPopup()
    {
        string href = SC_Common.BasePath + ControllerUrl + "/MenuPopup";
        Utils.OpenWinCenter("MenuPopup", href, 350, 400);
    }

    #endregion

    /// <summary>
    /// open Link Popup 
    /// </summary>
    /// <param name="link"></param>
    public static void OpenLinkPopup(string link)
    {
        int width = (Window.Screen.AvailWidth);
        int height = (Window.Screen.AvailHeight);

        Window.Open(link, "", "menubar=1,status=1,resizable=1,scrollbars=1,directories=1,location=1,width=" + width.ToString() + ",height=" + height.ToString() + ",left=" + 0 + ",top=" + 0);

    }
}
